import JsBase64 from 'js-base64'
import Cookies from 'js-cookie'

const USER_TOKEN = 'ChatRoom-TOKEN'
const USER_INFO = 'ChatRoom-USERINFO'
const USER_SETTING = 'ChatRoom_SETTING'

export function getToken() {
    return Cookies.get(USER_TOKEN)
}

export function setToken(token) {
    return Cookies.set(USER_TOKEN, token)
}

export function removeToken() {
    return Cookies.remove(USER_TOKEN)
}

export function getUser() {
    return Cookies.get(USER_INFO)
}

export function setUser(user) {
    return Cookies.set(USER_INFO, user)
}

export function removeUser() {
    return Cookies.remove(USER_INFO)
}

/**
 * 获取用户本地缓存的设置信息
 */
export function getUserSettingCache() {
    const data = localStorage.getItem(USER_SETTING)
    return data ? JSON.parse(data) : {}
}

/**
 * 用户设置保存到浏览器缓存中
 *
 * @param {Object} state 用户设置相关信息
 */
export function setUserSettingCache(state) {
    localStorage.setItem(USER_SETTING, JSON.stringify(state))
}

/**
 * 删除用户相关缓存信息
 */
export function removeAll() {
    localStorage.removeItem(USER_TOKEN)
    localStorage.removeItem(USER_INFO)
    localStorage.removeItem(USER_SETTING)
}
